package com.qf.fmall.untils;

import cn.hutool.jwt.JWT;


import java.util.Date;
public class TokenUtils {
    public static final String JWT_SECRET = "qfsy2305";

    public static final String JWT_ISSUER = "FMALL";

    /**
     * 生成token
     * @param username Token颁发给哪个用户
     * @param time 颁发时间
     * @return token 字符串
     */

        public static String createToken(String username,Integer time){
        //创建hutool提供的jwt对象
            JWT jwt = JWT.create();

        //1.服务器端自己私有的用来计算jwt的密钥
            jwt.setKey(JWT_SECRET.getBytes());
        //2.设置token颁发给哪个用户
            jwt.setSubject(username);
        //3.设置token由哪个机构颁发
            jwt.setIssuer(JWT_ISSUER);
        //4.颁发时间
            jwt.setIssuedAt(new Date());
        //5.过期时间
            jwt.setExpiresAt(new Date(System.currentTimeMillis()+1000*60*time));
            //利用jwt对象创建token字符
            String token =jwt.sign();
            return token;
        }



    public static boolean checkToken(String token){
        //根据token字符创建一个jwt对象
        JWT jwt =JWT.of(token);

        //设置 用来校验token的密钥
        jwt.setKey(JWT_SECRET.getBytes());

        //校验token
        boolean validate = jwt.validate(0);
        return validate;
    }


    public static void main(String[] args) {
// System.out.println(createToken("ZX", 10));
        String token="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJaWCIsImlzcyI6IkZNQUxMIiwiaWF0IjoxNzA1MzY4NjY0LCJleHAiOjE3MDUzNjkyNjR9.GA1gAHEzux2KO_fK3ENU3CZLPIe_pdgda0f0aZWsi3U";
        System.out.println(checkToken(token));
    }


    }
